<?php
	/**
	 * @author zhouguoyan
	 * @desc 公司搜索管理
	 * */	
class MailMagaController extends AppController
{				
	var $name = 'MailMaga';
    var $uses = array('Job','SysParams');
  	var $access = array ('search'=>array('role' => array('System')),
					     'showList'=>array('role' => array('System')),
					     'detail'=>array('role' => array('System')),
					     'dosave'=>array('role' => array('System'))
					      );      
	
	/**
	 * @author zhouguoyan
	 * @desc MailMaga首页
	 * */	
	function index(){
		
	}
	/**
	 * @author zhouguoyan
	 * @desc 公司搜索条件
	 * */	
	 function search(){
	 	
	 
		if(!empty($this->data)){
			$this->Session->write('mail_maga_search_condition',$this->data);
			$this->redirect('/MailMaga/showList');			
		}		
	
		$post_data = $this->SysParams->generateList("\"SysParams\".kbn = 'post_id' and \"SysParams\".deleted is null");
		$lang_data = $this->SysParams->generateList("\"SysParams\".kbn = 'hope_language_id' and \"SysParams\".deleted is null",'sort');
		$memb_type = $this->SysParams->generateList("\"SysParams\".kbn = 'hope_member_id' and \"SysParams\".deleted is null");
		$attention = $this->SysParams->generateList("\"SysParams\".kbn = 'attention_id' and \"SysParams\".deleted is null");
		$area_data = $this->SysParams->generateList("\"SysParams\".kbn = 'area_id' and \"SysParams\".deleted is null");
		$tema_data = $this->SysParams->generateList("\"SysParams\".kbn = 'tema_id' and \"SysParams\".deleted is null");
		
		$this->set('post_data',$post_data);
		$this->set('lang_data',$lang_data);
		$this->set('memb_data',$memb_type);
		$this->set('attention',$attention);
		$this->set('area_data',$area_data);
		$this->set('tema_data',$tema_data);	 	
	 }

	/**
	 * @author zhouguoyan
	 * @desc 公司搜索结果列表
	 * */	
	 function showList(){
		
		$condition = '';
		$search_data = $this->Session->read('mail_maga_search_condition');

		$condition = $this->Job->createMailCondition($this->Search,$search_data);
		$this->Session->write('excel_condition',$condition);
		$order = 'Job.modified desc';
		$count = $this->Job->findCount($condition);
		$this->setPaging('ajax','/mail_maga/showList',$count);		
		$data = $this->Job->findAll($condition,null,$order,$this->show,$this->page,2);
		if(empty($data) && empty($search_data)){
			$this->redirect('/MailMaga/search');
		}
		$this->setValue($data);
		$this->set('data',$data);
		$this->set('count',$count);	

	 }

	/**
	 * @author zhouguoyan
	 * @desc 公司搜索详细信息
	 * */		 
	function detail($job_id){
		
		$condition = "\"Job\".deleted is null and \"Job\".id=".$job_id;
		$data = $this->Job->find($condition,null,null,2);
		
		if(!empty($data['JobPickupArea'])){ 	
		 	$data['Job']['address1'] = trim($this->processInner2($data,'JobPickupArea','area_id'));
		 	$data['Job']['job_pickup_addr']=implode(',',$this->processInner1($data,'JobPickupArea','area_id'));
		}
		if(!empty($data['JobPickupThema'])){
		 	$data['Job']['tema1'] = trim($this->processInner2($data,'JobPickupThema','tema_id'));
		 	$data['Job']['job_pickup_tema']=implode(',',$this->processInner1($data,'JobPickupThema','tema_id'));
		}
	 	if(!empty($data['JobAttentionArea'])){
		 	$data['Job']['address2'] = trim($this->processInner2($data,'JobAttentionArea','area_id'));
		 	$data['Job']['job_attention_addr']=implode(',',$this->processInner1($data,'JobAttentionArea','area_id'));
	 	}
	 	if(!empty($data['JobAttentionThema'])){
		 	$data['Job']['tema2'] = trim($this->processInner2($data,'JobAttentionThema','tema_id'));
		 	$data['Job']['job_attention_tema']=implode(',',$this->processInner1($data,'JobAttentionThema','tema_id'));
	 	}
	 	if(!empty($data['Job']['attention_img_file'])){
	 		$data['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir').'/'.floor($data['Job']['id']/1000).'/'.$data['Job']['id'].'/'.$data['Job']['attention_img_file'];
	 	}
		$this->set('data',$data);
		$this->delCookie('mail_list');
		$this->set_Cookie("mail_list",$job_id,30);
		
		
	}
	 
	/**
	 * @author zhouguoyan
	 * @desc 保存公司信息
	 * */	
	function dosave($id=null){
		if(!empty($id)){
			$condition = "\"Job\".id = " . "'$id'";
		}else{
			$condition = $this->Session->read('excel_condition');
		}
		$order = 'Job.modified desc';
		$data = $this->Job->findAll($condition,null,$order,null,null,2);
		$this->delCookie('mail_list');
		$this->download($data);
		$this->Session->del('excel_condition');
		$this->Session->del('mail_maga_search_condition');
		
	}
	 
	function download($data){
		
		$file_name = "mailmage_".date("YmdHis").".txt";
		
		header("Content-Type:application/force-download");
		
		$data['Path']= 'http://' . FRONT_SERVER_NAME . '/docs/';
		header("Content-Disposition:attachment; filename=$file_name");
		$contents=$this->getMailTemplate("mail_txt_template",$data);
		exit();
	}
	
 
	function setValue(& $data) {

		$delete = array ();

		if (isset ($_COOKIE['mail_list']))
			$delete = split(',', $_COOKIE['mail_list']);
		
		foreach ($data as & $d) {
			$id = $d['Job']['id'];
			if (in_array($id, $delete)) {
				$d['Job']['delete'] = $id;
			}
		}
	}
	
	function processInner1($data,$inner,$field){
	    if(empty($data[$inner])){
	    	return array();
	    }	
	    $arr=array();
	    foreach($data[$inner] as $inner){
			$arr[]=$inner[$field];
		}
		return $arr;
	}
	
	function processInner2($data,$key,$kbn){
		if(empty($data[$key])){
			return '';
		}
		$arr = array();
		foreach($data[$key] as $d){
			$sysdata=null;
			if($key=='JobArea'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='area_id' and \"SysParams\".id=".$d[$kbn]);
			}elseif($key=='JobCategory'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='tema_id' and \"SysParams\".id=".$d[$kbn]);
			}else{
			    $sysdata=$this->SysParams->find('"SysParams".kbn='.'\''.$kbn.'\''.' and "SysParams".id='.$d[$kbn]);
			}
			if($key=='JobHopeLanguage'&&$d[$kbn]==4){
				$arr[] = trim($data['Job']['hope_language_note']);
			}else{
				$arr[] = trim($sysdata['SysParams']['name']);
			}
		}		
		return  implode(',',$arr);	
	}
}
?>